package com.songshuai.webapi.common.log;

import com.alibaba.fastjson.JSON;
import com.songshuai.webapi.common.mqchannel.OutputSource;
import org.slf4j.Marker;
import org.slf4j.event.Level;
import org.slf4j.event.SubstituteLoggingEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
@EnableBinding(OutputSource.class)
public class CustomLog {

    @Autowired
    @Output(OutputSource.CUSTPERMISSONCHANNEL)
    private MessageChannel custpermissonchannel;



    private void recordEvent(String className,Level level, String msg, Object[] args, Throwable throwable) {
        this.recordEvent(className,level, (Marker)null, msg, args, throwable);
    }

    private void recordEvent(String className,Level level, Marker marker, String msg, Object[] args, Throwable throwable) {
        SubstituteLoggingEvent loggingEvent = new SubstituteLoggingEvent();
        loggingEvent.setTimeStamp(System.currentTimeMillis());
        loggingEvent.setLevel(level);
        loggingEvent.setLoggerName(className);
        loggingEvent.setMessage(msg);
        loggingEvent.setArgumentArray(args);
        loggingEvent.setThrowable(throwable);
        loggingEvent.setThreadName(String.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal()));
        //this.eventQueue.add(loggingEvent);
       // System.out.println(JSON.toJSONString(loggingEvent));
        custpermissonchannel.send(MessageBuilder.withPayload(JSON.toJSONString(loggingEvent)).build());
    }



    public boolean isInfoEnabled() {
        return true;
    }

    public void info(String className,String msg) {
        this.recordEvent(className,Level.INFO, msg, (Object[])null, (Throwable)null);
    }

    public void info(String className,String format, Object arg) {
        this.recordEvent(className,Level.INFO, format, new Object[]{arg}, (Throwable)null);
    }

    public void info(String className,String format, Object arg1, Object arg2) {
        this.recordEvent(className,Level.INFO, format, new Object[]{arg1, arg2}, (Throwable)null);
    }

    public void info(String className,String format, Object... arguments) {
        this.recordEvent(className,Level.INFO, format, arguments, (Throwable)null);
    }

    public void info(String className,String msg, Throwable t) {
        this.recordEvent(className,Level.INFO, msg, (Object[])null, t);
    }

    public boolean isInfoEnabled(Marker marker) {
        return true;
    }

    public void info(String className,Marker marker, String msg) {
        this.recordEvent(className,Level.INFO, marker, msg, (Object[])null, (Throwable)null);
    }

    public void info(String className,Marker marker, String format, Object arg) {
        this.recordEvent(className,Level.INFO, marker, format, new Object[]{arg}, (Throwable)null);
    }

    public void info(String className,Marker marker, String format, Object arg1, Object arg2) {
        this.recordEvent(className,Level.INFO, marker, format, new Object[]{arg1, arg2}, (Throwable)null);
    }

    public void info(String className,Marker marker, String format, Object... arguments) {
        this.recordEvent(className,Level.INFO, marker, format, arguments, (Throwable)null);
    }

    public void info(String className,Marker marker, String msg, Throwable t) {
        this.recordEvent(className,Level.INFO, marker, msg, (Object[])null, t);
    }

    public boolean isWarnEnabled() {
        return true;
    }

    public void warn(String className,String msg) {
        this.recordEvent(className,Level.WARN, msg, (Object[])null, (Throwable)null);
    }

    public void warn(String className,String format, Object arg) {
        this.recordEvent(className,Level.WARN, format, new Object[]{arg}, (Throwable)null);
    }

    public void warn(String className,String format, Object arg1, Object arg2) {
        this.recordEvent(className,Level.WARN, format, new Object[]{arg1, arg2}, (Throwable)null);
    }

    public void warn(String className,String format, Object... arguments) {
        this.recordEvent(className,Level.WARN, format, arguments, (Throwable)null);
    }

    public void warn(String className,String msg, Throwable t) {
        this.recordEvent(className,Level.WARN, msg, (Object[])null, t);
    }

    public boolean isWarnEnabled(Marker marker) {
        return true;
    }

    public void warn(String className,Marker marker, String msg) {
        this.recordEvent(className,Level.WARN, msg, (Object[])null, (Throwable)null);
    }

    public void warn(String className,Marker marker, String format, Object arg) {
        this.recordEvent(className,Level.WARN, format, new Object[]{arg}, (Throwable)null);
    }

    public void warn(String className,Marker marker, String format, Object arg1, Object arg2) {
        this.recordEvent(className,Level.WARN, marker, format, new Object[]{arg1, arg2}, (Throwable)null);
    }

    public void warn(String className,Marker marker, String format, Object... arguments) {
        this.recordEvent(className,Level.WARN, marker, format, arguments, (Throwable)null);
    }

    public void warn(String className,Marker marker, String msg, Throwable t) {
        this.recordEvent(className,Level.WARN, marker, msg, (Object[])null, t);
    }

    public boolean isErrorEnabled() {
        return true;
    }

    public void error(String className,String msg) {
        this.recordEvent(className,Level.ERROR, msg, (Object[])null, (Throwable)null);
    }

    public void error(String className,String format, Object arg) {
        this.recordEvent(className,Level.ERROR, format, new Object[]{arg}, (Throwable)null);
    }

    public void error(String className,String format, Object arg1, Object arg2) {
        this.recordEvent(className,Level.ERROR, format, new Object[]{arg1, arg2}, (Throwable)null);
    }

    public void error(String className,String format, Object... arguments) {
        this.recordEvent(className,Level.ERROR, format, arguments, (Throwable)null);
    }

    public void error(String className,String msg, Throwable t) {
        this.recordEvent(className,Level.ERROR, msg, (Object[])null, t);
    }

    public boolean isErrorEnabled(Marker marker) {
        return true;
    }

    public void error(String className,Marker marker, String msg) {
        this.recordEvent(className,Level.ERROR, marker, msg, (Object[])null, (Throwable)null);
    }

    public void error(String className,Marker marker, String format, Object arg) {
        this.recordEvent(className,Level.ERROR, marker, format, new Object[]{arg}, (Throwable)null);
    }

    public void error(String className,Marker marker, String format, Object arg1, Object arg2) {
        this.recordEvent(className,Level.ERROR, marker, format, new Object[]{arg1, arg2}, (Throwable)null);
    }

    public void error(String className,Marker marker, String format, Object... arguments) {
        this.recordEvent(className,Level.ERROR, marker, format, arguments, (Throwable)null);
    }

    public void error(String className,Marker marker, String msg, Throwable t) {
        this.recordEvent(className,Level.ERROR, marker, msg, (Object[])null, t);
    }
}